<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Statistics_m extends MY_Model {

	public function __construct()
	{
		parent::__construct();
		$this->load->library('module_helper');
	}
	
	function show(){
		$records  = array();
		if(!isset($_GET['c'])){
			if(!isset($_GET['key']) || $_GET['filter'] == 'y'){
				$year = (isset($_GET['key'])) ? intval($_GET['key']) : date('Y',time());
				$records = $this->db->where("YEAR( FROM_UNIXTIME( `created_time` ) ) = $year")
							->order_by('created_time','desc')
							->get(TBL_SHOW)
							->result();
			}elseif($_GET['filter'] == 'buyer_email'){
				$BUYER = $this->user_entity->init('email',$_GET['key']);
				if(!$BUYER){return  array();}
			
				$bidArray = $this->bid_entity->getMyBid($BUYER->getAccCode());
				if(empty($bidArray)) return  array();
			 
				$showArray = array();
				foreach($bidArray as $item){
					$showArray[] = $item->show_ref_no;
				}
				
				$records = $this->db->where("ref_no IN (".implode(',',$showArray).")")
							->order_by('created_time','desc')
							->get(TBL_SHOW)
							->result();
			}elseif($_GET['filter'] == 'seller_email'){
				$SELLER = $this->user_entity->init('email',$_GET['key']);
				if(!$SELLER){return  array();}
				$records = $this->db->where("user_created_id",$SELLER->getId())
							->order_by('created_time','desc')
							->get(TBL_SHOW)
							->result();
			}			
		}else{
			$code = $_GET['c'];
			$records = $this->db->where("country_origin_code LIKE '$code'")
							->order_by('created_time','desc')
							->get(TBL_SHOW)
							->result();
		}						
		return $records;														
	}
	
	function successfully_transaction_shows(){
		$records  = array();
		$year = (isset($_GET['key'])) ? intval($_GET['key']) : date('Y',time());
		$records = $this->db->where("YEAR( FROM_UNIXTIME( `created_time` ) ) = $year AND pay_status LIKE 'completed'")
					->order_by('created_time','desc')
					->get(TBL_SHOW)
					->result();
		return $records;
	}
	
	function summary_report(){
		$totalBuyers = count( $this->db->where('acc_type','buyer')->get(TBL_ACCOUNT)->result());
		$totalSellers = count( $this->db->where('acc_type','seller')->get(TBL_ACCOUNT)->result());
		return array('buyerNo'=>$totalBuyers, 'sellerNo'=>$totalSellers);
	}
	
	
	
	
}	